﻿using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace NMA.Infrastructure.NHibernate.Tests.Specification
{
    using NMA.Domain.Shared.Specification;
    using NMA.Infrastructure.NHibernate.Service.Event;
    using NMA.Domain.Model.Repository;
    using NMA.Infrastructure.NHibernate.Service.Specification;    
    using NMA.Domain.Model;    
    using NMA.Infrastructure.NHibernate.DomainObject;    

    [TestFixture] 
    public class CategoryAuditSpecTest : TestingBase
    {
        private ISpecification<AuditEvent> _specification = null;
        private IUserRepository _userRepository = null;
        
        protected override void  Before_each_test()
        {
 	        base.Before_each_test();
        
            _userRepository= MockRepository.GenerateMock<IUserRepository>();
            _specification = new CategoryAuditSpecification(_userRepository);
        }

        [Test]
        [ExpectedException]
        public void Will_throw_exception_when_category_audit_event_is_null()
        {
            _specification.IsSatisfiedBy(null);            
        }

        [Test]
        [ExpectedException]
        public void Will_throw_exception_when_dont_have_user_repository()
        {
            AuditEvent catEvent = new AuditEvent()
            {
                UserName = "username",
                UserAction = "user action",
                 DateEvent = DateTime.Now
            };

            _specification.IsSatisfiedBy(catEvent);
        }

        [Test]
        public void Can_satisfied_when_the_user_name_is_valid()
        {
            AuditEvent catEvent = new AuditEvent()
            {
                UserName = "username",
                UserAction = "user action",
                DateEvent = DateTime.Now
            };

            IEnumerable<IUser> list = new List<IUser>() { new User() {
                 Id = Guid.NewGuid(),
                 UserName = "username",
                 IsAlive = true,
                 Description = "description",
                 Password = "password",
                 CreatedDate = DateTime.Now,
                 UpdatedDate = DateTime.Now,
                 BelongToRole = 0,
                 Address = null
            }};                       

            // mock the user repository with stub the Expression<Func...>...
            _userRepository.Stub(c => c.GetBy(Arg<Expression<Func<IUser, bool>>>.Is.NotNull)).Return(list);

            var result = _specification.IsSatisfiedBy(catEvent);            

            Assert.IsTrue(result);
        }
        
        protected override void After_each_test()
        {
            base.After_each_test();

            if (_userRepository != null)
                GC.SuppressFinalize(_userRepository);
            if (_specification != null)
                GC.SuppressFinalize(_specification);
        }
    }
}